/**
 * @file    hal_gpio.c
 * @author  Azolla (1228449928@qq.com)
 * @brief    
 * @version 0.1
 * @date    2022-12-06
 * 
 * @copyright Copyright (c) 2022
 * */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"

#include "hal_config.h"
#include "hal_gpio.h"
  
//=============================================================================
const uint8_t gpio_out_pin[GPIO_OUT_PIN_NUM] = GPIO_OUT_PIN_TABLE;
const uint8_t gpio_in_pin[GPIO_IN_PIN_NUM] = GPIO_IN_PIN_TABLE;
//=============================================================================
 
void gpio_output_init(void)
{
#if GPIO_OUT_PIN_NUM > 0
    gpio_config_t io_conf;
    // disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    // set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    // bit mask of the pins that you want to set,e.g.
    io_conf.pin_bit_mask = 0;  // clear zero. 
    for (uint8_t i = 0; i < GPIO_OUT_PIN_NUM; i++) {
        io_conf.pin_bit_mask |= BIT64(gpio_out_pin[i]);
    }
    // disable pull-down mode
    io_conf.pull_down_en = false;
    // disable pull-up mode
    io_conf.pull_up_en = false;
    // configure GPIO with the given settings
    gpio_config(&io_conf);

    // gpio_set_direction(GPIO_NUM_16, GPIO_MODE_OUTPUT);
    // gpio_set_level(GPIO_NUM_16, 1);
#endif
}

static void gpio_input_init(void)
{
#if GPIO_IN_PIN_NUM > 0
    gpio_config_t io_conf;
    // disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    // set as output mode
    io_conf.mode = GPIO_MODE_INPUT;
    // bit mask of the pins that you want to set,e.g.
    io_conf.pin_bit_mask = 0;  // clear zero. 
    for (uint8_t i = 0; i < GPIO_IN_PIN_NUM; i++) {
        io_conf.pin_bit_mask |= BIT64(gpio_in_pin[i]);
    }
    // disable pull-down mode
    io_conf.pull_down_en = false;
    // disable pull-up mode
    io_conf.pull_up_en = true;
    // configure GPIO with the given settings
    gpio_config(&io_conf);

    // config gpio input mode.
    // gpio_set_direction(CONFIG_RMT_RX_GPIO, GPIO_MODE_INPUT);
    // gpio_set_pull_mode(CONFIG_RMT_RX_GPIO, GPIO_PULLUP_ONLY);
    // gpio_pullup_en(CONFIG_RMT_RX_GPIO);
#endif
}


// GPIO简单的输入输出初始化
void hal_gpio_init(void)
{
    esp_log_level_set("gpio", ESP_LOG_WARN);
    gpio_output_init();
    gpio_input_init(); 
}
 